#include "Tank.h"
#include <string>

Tank::Tank() {

	tankSurface = NULL;
	goingBackwards = false;
	goingForward = false;
	turningLeft = false;
	turningRight = false;
	shooting = false;
	keyPressed = false;
	angle = 0.0;

}

Tank::Tank(int posX, int posY, Widget *parent, std::string owner,int angle) :
		Widget(posX, posY, parent) {

	std::stringstream ss;
	ss << "owner: " << owner;
//	Logger::log("tank", "constructor", &ss);

	this->owner = owner;

	tankSurface = loadSurface("./trunk/res/img/tanks.png");
	parcel = getPosition();
	parcel.height = 30;
	parcel.width = 30;
	nextX = parcel.posX;
	nextY = parcel.posY;

	goingBackwards = false;
	goingForward = false;
	turningLeft = false;
	turningRight = false;
	shooting = false;
	this->angle = angle;
	keyPressed = false;

}

void Tank::handleEvent(Event event) {
	/*std::stringstream owner;
	 owner << "owner: " << this->owner;

	 Logger::log("tank", "handleEvent", &owner);*/

	if ((event.getSender() == this->owner) || duenoesinstancialocal) {

		if (event.getType() == UserEvent) {

			Logger::log("tank", "handleEvent", "entro al handle del tanque");

			if (event.getintData2() == KeyDown) {
				if (event.getEvent()->user.code == KeyPressed) {
					goingBackwards = true;
				} else if (event.getEvent()->user.code == KeyReleased) {
					goingBackwards = false;
				}
			}

			else if (event.getintData2() == KeyUp) {
				if (event.getEvent()->user.code == KeyPressed) {
					Logger::log("Tank", "handleEvent", "toma el evento");
					goingForward = true;
				} else if (event.getEvent()->user.code == KeyReleased) {
					goingForward = false;
				}
			}

			else if (event.getintData2() == KeyLeft) {
				if (event.getEvent()->user.code == KeyPressed) {
					turningLeft = true;
				} else if (event.getEvent()->user.code == KeyReleased) {
					turningLeft = false;
				}
			}

			else if (event.getintData2() == KeyRight) {
				if (event.getEvent()->user.code == KeyPressed) {
					turningRight = true;
				} else if (event.getEvent()->user.code == KeyReleased) {
					turningRight = false;
				}
			}

			else if (event.getintData2() == Space) {
				if (event.getEvent()->user.code == KeyPressed) {
					shooting = true;
				} else if (event.getEvent()->user.code == KeyReleased) {
					shooting = false;
				}
			}
		} else {

			//	keyState = event.getType();
			switch (event.getKey()) {
			case KeyDown:
				if (event.getType() == KeyPressed) {
					goingBackwards = true;
				} else if (event.getType() == KeyReleased) {
					goingBackwards = false;
				}
				keyPressed = false;
				break;
			case KeyUp:
				if (event.getType() == KeyPressed) {
					goingForward = true;
				} else if (event.getType() == KeyReleased) {
					goingForward = false;
				}
				keyPressed = false;
				break;
			case KeyLeft:
				if (event.getType() == KeyPressed) {
					turningLeft = true;
				} else if (event.getType() == KeyReleased) {
					turningLeft = false;
				}
				keyPressed = false;
				break;
			case KeyRight:
				if (event.getType() == KeyPressed) {
					turningRight = true;
				} else if (event.getType() == KeyReleased) {
					turningRight = false;
				}
				keyPressed = false;
				break;
			case Space:
				if ((event.getType() == KeyPressed) && (keyPressed == false)) {
					shooting = true;
					keyPressed = true;
					// TODO mandar evento del disparo al servidor
				} else if ((keyPressed == true)
						&& (event.getType() == KeyReleased)) {
					keyPressed = false;
					shooting = false;
				}
				break;
			default:
				break;
			}
		}

	}
}

void Tank::resolve() {
	if (turningLeft) {
		if (angle == 350.0) {
			angle = 0.0;
		} else {
			angle += 10.0;
		}

		std::stringstream ss;
//		ss << "angulo despues de rotar a la izq: " << angle;

//		Logger::log("tank", "resolve", &ss);

	}

	if (turningRight) {
		if (angle == 0.0) {
			angle = 350.0;
		} else {
			angle -= 10.0;
		}

		std::stringstream ss;
//		ss << "angulo despues de rotar a la der: " << angle;

//		Logger::log("tank", "resolve", &ss);

	}

	if (goingForward) {
		if ((angle >= 0.0) && (angle <= 90.0)) {

			int avanceY, avanceX;

			avanceY = (-(angle / 10));
			avanceX = avanceY + 9;

			nextX = parcel.posX + avanceX;
			nextY = parcel.posY + avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

//			Logger::log("tank", "resolve", &ss1);

		} else if ((angle > 90.0) && (angle <= 180.0)) {

			int avanceY, avanceX;

			avanceX = (-(angle - 90)) / 10;
			avanceY = -avanceX - 9;

			nextX = parcel.posX + avanceX;
			nextY = parcel.posY + avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

//			Logger::log("tank", "resolve", &ss1);

		} else if ((angle > 180) && (angle <= 270)) {

			int avanceY, avanceX;

			avanceY = (angle - 180) / 10;
			avanceX = avanceY - 9;

			nextX = parcel.posX + avanceX;
			nextY = parcel.posY + avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

//			Logger::log("tank", "resolve", &ss1);

		} else if ((angle >= 270) && (angle <= 360)) {

			int avanceY, avanceX;

			avanceX = ((angle - 270) / 10);
			avanceY = -avanceX + 9;

			nextX = parcel.posX + avanceX;
			nextY = parcel.posY + avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

//			Logger::log("tank", "resolve", &ss1);

		}
	}
	if (goingBackwards) {
		if ((angle >= 0.0) && (angle <= 90.0)) {

			int avanceY, avanceX;

			avanceY = (-(angle / 10));
			avanceX = avanceY + 9;

			nextX = parcel.posX - avanceX;
			nextY = parcel.posY - avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

			Logger::log("tank", "resolve", &ss1);

		} else if ((angle > 90.0) && (angle <= 180.0)) {

			int avanceY, avanceX;

			avanceX = (-(angle - 90)) / 10;
			avanceY = -avanceX - 9;

			nextX = parcel.posX - avanceX;
			nextY = parcel.posY - avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

//			Logger::log("tank", "resolve", &ss1);

		} else if ((angle > 180) && (angle <= 270)) {

			int avanceY, avanceX;

			avanceY = (angle - 180) / 10;
			avanceX = avanceY - 9;

			nextX = parcel.posX - avanceX;
			nextY = parcel.posY - avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

			Logger::log("tank", "resolve", &ss1);

		} else if ((angle >= 270) && (angle <= 360)) {

			int avanceY, avanceX;

			avanceX = ((angle - 270) / 10);
			avanceY = -avanceX + 9;

			nextX = parcel.posX - avanceX;
			nextY = parcel.posY - avanceY;

			std::stringstream ss1;
//			ss1 << "nextX = " << nextX << " - nextY = " << nextY;

//			Logger::log("tank", "resolve", &ss1);

		}
	}
	if (nextX > 10 && nextX + parcel.width < 810) {
		parcel.posX = nextX;
	}
	if (nextY > 10 && nextY + parcel.height < 610) {
		parcel.posY = nextY;
	}
}

void Tank::showOn(SDL_Surface* surface) {
	SDL_Rect rectSrc;
	rectSrc.x = angle * 3;
	rectSrc.y = 0;
	rectSrc.w = parcel.width;
	rectSrc.h = parcel.height;

	SDL_Rect rectDest;
	rectDest.x = parcel.posX;
	rectDest.y = parcel.posY;
	rectDest.w = parcel.width;
	rectDest.h = parcel.height;

	SDL_BlitSurface(tankSurface, &rectSrc, surface, &rectDest);
}

int Tank::getBulletAngle() {
	return angle;
}

bool Tank::isShooting() {
	bool result = this->shooting;
	this->shooting = false;
	return result;
}

Tank::~Tank() {
	SDL_FreeSurface(tankSurface);
}


int Tank::getbulletX() {
	return (parcel.posX - 10);
}

int Tank::getbulletY() {
	return (parcel.posY - 10);
}

std::string Tank::getOwner() {
	return owner;
}

TankStatus Tank::getTankStatus() {
	Parcel parcela;
	parcela = getPosition();

	return TankStatus(owner, parcela.posX, parcela.posY, angle);
}
